<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\mvc\service;

use suifeng\mvc\Service;

class ModelService extends Service
{
    protected $model;

    public function save($data)
    {
        $pk = model($this->model)->getPk();
        if (isset($data[$pk]) && $data[$pk]) {
            model($this->model)->insert($data);
        } else {
            model($this->model)->update($data);
        }
    }

    public function delete($data)
    {
        return $this->_delete($this->model_name, $data);
    }

    public function fetch_list($where = null, $order = null, $page = null, $length = null)
    {
        $query = model($this->model)->db();
        $where && $query->where($where);
        $order && $query->order($order);
        $page && $length && $query->page($page, $length);
        return $query->select()->toArray();
    }

    public function fetch_one($id)
    {
        return model($this->model)->get($id);
    }
}